import { createRouter, createWebHistory } from "vue-router";
import VueCookies from "vue-cookies";
const routes = [
  {
    name: "登录",
    path: "/login",
    component: () => import("../views/Login.vue"),
  },
  {
    name: "框架页面",
    path: "/",
    component: () => import("../views/Framework.vue"),
    redirect: "/blog/list",
    children: [
      {
        path: "/blog/test",
        name: "test",
        component: () => import("../views/blog/Test.vue"),
      },
      {
        path: "/blog/list",
        name: "博客管理",
        component: () => import("../views/blog/BlogList.vue"),
      },
      {
        path: "/blog/category",
        name: "博客分类",
        component: () => import("../views/blog/BlogCategory.vue"),
      },
      {
        path: "/special/list",
        name: "专题",
        component: () => import("../views/special/SpecialList.vue"),
      },
      {
        path: "/setting/my",
        name: "个人信息设置",
        component: () => import("../views/settings/MyInfo.vue"),
      },
      {
        path: "/setting/user",
        name: "成员管理",
        component: () => import("../views/settings/TeamUser.vue"),
      },
      {
        path: "/setting/sysSetting",
        name: "系统设置",
        component: () => import("../views/settings/SysSetting.vue"),
      },
      {
        path: "/recovery/list",
        name: "回收站",
        component: () => import("../views/recovery/RecoveryList.vue"),
      },
    ],
  },
];

const router = createRouter({
  routes,
  history: createWebHistory(),
});

// 避免还没登录就访问管理页面
// 每次访问路由都要做一下这个检查，只要是还没登陆就访问非login的，都跳转到login
router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo");
  if (!userInfo && to.path != "/login") {
    router.push("/login");
  }
  next();
});
export default router;
